/*
参数1 data对象
 data导入的数据类型 一个数组对象
 heads头部名称
 columns要输出的字段名
 remove_targets是否需要过滤html标签
 spe_column需要处理的数字字段 会在之前加`
 例子：
{
 'data':[{id:1,name:'a',sort:10},{id:2,name:'b',sort:9},{id:3,name:'c',sort:8}],
 'heads':['id号','姓名'],
 'columns':['id','name'],
 'remove_targets':false,
 'spe_column':['id'],
}
参数2 fun获取csv内容后的回调函数
*/
var data2csv = function (data = {}, fun = function (str) {
  return str
}) {
  //验证并处理参数
  if (data.heads.length != data.columns.length) {
    return {'code': false, 'msg': '表头和内容字段数不符'};
  }
  if (!data.remove_targets) {
    data.remove_targets = false;
  } else {
    data.remove_targets = true;
  }
  csv_data = data.data;
  heads = data.heads;
  columns = data.columns;
  spe_column = data.spe_column;
  remove_targets = data.remove_targets;
  // 构造csv内容
  var str = "";
  for (var j in csv_data) {
    e = csv_data[j];
    for (var i in columns) {
      c = e[columns[i]];
      if (remove_targets) {
        // c = c.replace(/<[^>]+>/g, "") + ",";
          c = c.toString().replace(/<[^>]+>/g, "") + ",";
      }
      if (spe_column.length > 0 && spe_column.indexOf(columns[i]) > -1) {
        // c = '`'+c;//表格前添加自定义内容
      }
      str += c;
    }
    str += "\n";
  }
  head_str = "";
  for (var i in heads) {
    head_str += heads[i] + ",";
  }
  head_str += "\n";
  str = head_str + str;
  return fun(str);
};
//生成并下载文件
function download_file(file_name, content, o = true) {
  if (o) {
    content = '\ufeff' + content
  }
  var aTag = document.createElement('a');
  var blob = new Blob([content]);
  aTag.download = file_name;
  aTag.href = URL.createObjectURL(blob);
  aTag.click();
  URL.revokeObjectURL(blob);
  isDownLoadCSV = true;//防止重复点击-导出检索结果-csv表格
};
/*参数*/
// var csv = [
//   {
//     'io': '0',
//     'num': '10',
//     'em_name': '0',
//     'money': '0',
//   }
// ];
// var head = [
//   '交易类型',
//   '订单号',
//   '操作人姓名及编号',
//   '原价总额',
// ];
// var column = [
//   'io',
//   'num',
//   'em_name',
//   'money'
// ];
// var num_column = ['num'];
// data2csv({
//   'data': csv,
//   'heads': head,
//   'columns': column,
//   'spe_column': num_column,
//   'remove_targets': true,
// }, function (content) {
//   console.log(content);
//   // download_file('order_list.csv',content,true)
//   $('#out2csv').on('click', function () {
//     download_file('order_list.csv', content, true)
//   });
// });

/*
var csv = [
  {
    'io': '0',
    'num': '10',
    'em_name': '0',
    'money': '0',
  },
  {
    'io': '20',
    'num': '10',
    'em_name': '20',
    'money': '40',
  }
];
var head = [
  '交易类型',
  '订单号',
  '操作人姓名及编号',
  '原价总额',
];
var column = [
  'io',
  'num',
  'em_name',
  'money'
];
var num_column = ['num'];
var $data={
  'data': csv,
  'heads': head,
  'columns': column,
  'spe_column': num_column,
  'remove_targets': true,
};
var $fun =function($file_name,content){
  $('#out2csv').on('click', function () {
    download_file($file_name, content, true)
  });
}
function  downLoadCSV($data,$file_name,$fun){
  data2csv($data, function (content) {
    console.log(content);
    // download_file('order_list.csv',content,true)
    $fun($file_name,content)//
  });
};
downLoadCSV($data,'order_list.csv',$fun);
*/